﻿
Imports System.IO
Imports System.Net
Imports usshare

Public Module wmusclnt

    Private supportButton As Boolean = False

#If DEBUG Then
    Public Const homepageUrl As String = "http://rlss.in:800/"
    Public Const baseUrl As String = "http://rlss.in:800/xmlapi/?"
#Else
    Public Const homepageUrl As String = "http://rlss.in/"
    Public Const baseUrl As String = "http://rlss.in/xmlapi/?"
#End If
    Public ReadOnly redirectorBeginTag As String = "<" + xmlapi.submitResponse.redirector + ">"
    Public ReadOnly redirectorEndTag As String = "</" + xmlapi.submitResponse.redirector + ">"
#If RETIRED Then
    Private WithEvents browser As WebBrowser = Nothing
    Private done As Boolean = False
#End If

    Public Function smartphone() As Boolean
        Return Not supportButton
    End Function

    Public Function ppc() As Boolean
        Return supportButton
    End Function

    Public Sub readclpClick(ByVal url As TextBox)
        assert(Not url Is Nothing, "url is nothing.")
        Try
            url.Text = Convert.ToString(Clipboard.GetDataObject().GetData(DataFormats.Text))
        Catch
            url.Text = "<Cannot read from clipboard.>"
        End Try
    End Sub

    Public Sub writeclpClick(ByVal shorturl As TextBox)
        assert(Not shorturl Is Nothing, "shorturl is nothing.")
        Try
            Clipboard.SetDataObject(shorturl.Text)
        Catch
        End Try
    End Sub

    Public Function generateRequestUrl(ByVal url As TextBox, ByVal shortform As TextBox) As String
        assert(Not url Is Nothing AndAlso Not shortform Is Nothing, "url or shortform is nothing.")
        Dim rtn As String = Nothing
        Dim u As String = Nothing
        Dim s As String = Nothing
        copy(u, url.Text)
        copy(s, shortform.Text)
        rtn = baseUrl + query.url + "=" + urlUtils.urlencode(u)
        If strlen(s) > 0 Then
            rtn += "&" + query.key + "=" + urlUtils.urlencode(s)
        End If
        rtn += "&" + query.action + "=" + actionVerbs(action.submit) + "&src=wmusclient"

        Return rtn
    End Function

    Public Sub shortitClick(ByVal url As TextBox, ByVal shortform As TextBox, ByVal shorturl As TextBox)
        assert(Not shorturl Is Nothing, "shorturl is nothing.")
#If RETIRED Then
        assert(Not browser Is Nothing, "browser is nothing.")
#End If
        On Error Resume Next

        If url.Text.Length = 0 Then
            MsgBox("please provide a url first.")
            Exit Sub
        End If

        Cursor.Current = Cursors.WaitCursor
        Cursor.Show()
        Dim result As String = Nothing
        If readHttpWebBypassWML(generateRequestUrl(url, shortform), result) AndAlso Not isemptystring(result) Then
#If RETIRED Then
        done = False
        browser.Navigate(generateRequestUrl(url, shortform))
        While Not done
            Application.DoEvents()
            sleep(10)
        End While
        Dim result As String = Nothing
        result = browser.ControlCollection()
        If Not isemptystring(result) Then
#End If
            Dim failed As Boolean = False
            Dim index1 As Int32
            Dim index2 As Int32
            index1 = strindexof(result, redirectorBeginTag)
            If index1 <> npos Then
                'jump "<redirector>"
                index1 += strlen(redirectorBeginTag)
                index2 = result.IndexOf(redirectorEndTag, index1)
                If index2 > index1 Then
                    shorturl.Text = strmid(result, index1, index2 - index1)
                    urlUtils.urldecode(shorturl.Text)
                    kickLeftRightBlank(shorturl.Text)
                    writeclpClick(shorturl)
                Else
                    failed = True
                End If
            Else
                failed = True
            End If

            If failed Then
                shorturl.Text = "FAILED" + vbCrLf + result
            End If
        Else
            shorturl.Text = "<Failed to get response from server.>"
        End If
        Cursor.Current = Cursors.Default
        Cursor.Hide()
    End Sub

    Public Sub initial()
#If RETIRED Then
        browser = New WebBrowser()
#End If
    End Sub

    Public Sub main(ByVal args() As String)
        Dim b As Button = Nothing
        Try
            b = New Button()
            supportButton = True
        Catch
            supportButton = False
        End Try

        If ppc() Then
            Application.Run(New ppc())
        Else
            Application.Run(New sp())
        End If
    End Sub

#If RETIRED Then
    Private Sub browser_DocumentCompleted(ByVal sender As Object, _
                                          ByVal e As WebBrowserDocumentCompletedEventArgs) _
                                          Handles browser.DocumentCompleted
        done = True
    End Sub
#End If
End Module
